package com.wcy.entity.userVo;

import com.wcy.entity.Role;
import com.wcy.entity.User;
import com.wcy.entity.UserRole;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//返回一个普通的用户信息、不带有敏感信息
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserFiltered {
    private Integer userId;
    private String avatar;
    private String username;
    private String email;
    private String phone;
    private List<UserRole> roles;
    private String validateToken;

    public UserFiltered(Map<String, Object> map) {
        this.userId = (Integer) map.get("userId");
        this.avatar = (String) map.get("avatar");
        this.username = (String) map.get("username");
        this.email = (String) map.get("email");
        this.phone = (String) map.get("phone");
        this.roles = (List<UserRole>) ((List)map.get("roles")).stream().map(e->new UserRole((Map)e)).collect(Collectors.toList());
        this.validateToken = (String) map.get("validateToken");
    }

    public UserFiltered(User user,String validateToken) {
        this.userId = user.getUserId();
        this.avatar = user.getAvatar();
        this.username = user.getUsername();
        this.email = user.getEmail();
        this.phone = user.getPhone();
        this.roles = user.getRoles();
        this.validateToken = validateToken;//用于存储一次性凭据，验证用户身份
    }

    public UserFiltered(User user) {
        this.userId = user.getUserId();
        this.avatar = user.getAvatar();
        this.username = user.getUsername();
        this.email = user.getEmail();
        this.phone = user.getPhone();
    }
}
